package org.yi.utils;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

public class MapUtils {

	public void toPojo(Map<String, String> m, Object obj) throws Exception {
		try {
			String s = obj.getClass().getName();
			Class<?> clazz = Class.forName(s);
			for (Entry<String, String> entry : m.entrySet()) {
				Field field = clazz.getDeclaredField((String) entry.getKey());
				field.setAccessible(true);

				if (field.getType() == int.class) {
					field.set(obj, Integer.parseInt((String) entry.getValue()));
				} else if (field.getType() == String.class) {
					field.set(obj, (String) entry.getValue());
				} else if (field.getType() == Date.class) {
					// field.set(obj, DateUtility.parseStrToDate((String)entry
					// .getValue()));
				} else if (field.getType() == Double.class) {
					field.set(obj, Double.parseDouble((String) entry.getValue())); // 由于时间类型比较特殊，所以我自己写了个方法处理时间类型，这一段不能直接用，可以自己写个方法替掉。
				} else if (field.getType() == Float.class) {
					field.set(obj, Float.parseFloat((String) entry.getValue()));
				} else if (field.getType() == Long.class) {
					field.set(obj, Long.parseLong((String) entry.getValue()));
				} else if (field.getType() == Boolean.class) {
					field.set(obj, Boolean.parseBoolean((String) entry.getValue()));
				} else if (field.getType() == Short.class) {
					field.set(obj, Short.parseShort((String) entry.getValue()));
				}
			}
		} catch (Exception e) {
			throw e;
		}
	}
}
